package com.mylib.libcore.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.bean.LocalCityBean;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: cl
 * @CreateDate: 2020/11/24 15:51
 * @UpdateRemark:
 */
public class CityDataUtils {

     public static String jsonCityString = "";
     public static List<LocalCityBean> locationList = new ArrayList<>();
     public static List<LocalCityBean> listProvince = new ArrayList<>();
    public static List<LocalCityBean> listCity = new ArrayList<>();
    public static List<LocalCityBean> listStreet = new ArrayList<>();

    public static String getJsonCityString() {
        return jsonCityString;
    }

    public static void setJsonCityString(String jsonCityString) {
        CityDataUtils.jsonCityString = jsonCityString;
    }

    public static List<LocalCityBean> getLocationList() {
        return locationList;
    }

    public static void setLocationList(List<LocalCityBean> locationList) {
        CityDataUtils.locationList = locationList;
    }

    public static List<LocalCityBean> getListProvince() {
        return listProvince;
    }

    public static void setListProvince(List<LocalCityBean> listProvince) {
        CityDataUtils.listProvince = listProvince;
    }

    public static List<LocalCityBean> getListCity() {
        return listCity;
    }

    public static void setListCity(List<LocalCityBean> listCity) {
        CityDataUtils.listCity = listCity;
    }

    public static List<LocalCityBean> getListStreet() {
        return listStreet;
    }

    public static void setListStreet(List<LocalCityBean> listStreet) {
        CityDataUtils.listStreet = listStreet;
    }

    /**
     * 从asset目录下读取城市json文件转化为String类型
     */
    public static void getLocalCityJson(Context context) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            InputStream is = context.getAssets().open("citydata.json");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
            is.close();
            jsonCityString = stringBuilder.toString();
            if(!UIUtils.isEmpty(jsonCityString)){
                Gson gson = new Gson();
                locationList = gson.fromJson(jsonCityString, new TypeToken<List<LocalCityBean>>() {}.getType());
            }
            initProvinceList();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void initProvinceList() {
        listProvince.clear();
        if(!UIUtils.isEmpty(locationList) && locationList.size()>0){
            for(LocalCityBean item:locationList){
                listProvince.add(item);
            }
        }

    }

    public static void initCityList(String code) {
        if(UIUtils.isEmpty(code)) return;
        listCity.clear();
        if(!UIUtils.isEmpty(listProvince) && listProvince.size()>0){
            for(LocalCityBean item:listProvince){
                if(!UIUtils.isEmpty(item ) &&  !UIUtils.isEmpty(item.getCode()) && code.equals(item.getCode())){
                    listCity= item.getChildren();
                    return;
                }
            }

        }
    }

    public static void initStreetList(String code) {
        if(UIUtils.isEmpty(code)) return;
        listStreet.clear();
        if(!UIUtils.isEmpty(listCity) && listCity.size()>0){
            for(LocalCityBean item:listCity){
                if(!UIUtils.isEmpty(item) && !UIUtils.isEmpty(item.getCode()) && code.equals(item.getCode())){
                    listStreet=item.getChildren();
                    return;
                }
            }

        }
    }
}
